package ifpb.pp.rh.report;

import java.io.InputStream;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 *
 * @author Jarddel Antunes
 */


public class GerarRelatorio {

    public void relatorio(List lista) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
            ServletOutputStream responseStream = response.getOutputStream();

            // caminho do jrxml
            InputStream pathjrxml = getClass().getResourceAsStream("../jrxml/servidores.jrxml");
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "attachment; filename=\"relatorio.pdf\"");
            JasperReport pathReport = JasperCompileManager.compileReport(pathjrxml);
            JasperPrint print = JasperFillManager.fillReport(pathReport, null,
                    new JRBeanCollectionDataSource(lista));
            JasperExportManager.exportReportToPdfStream(print, responseStream);

            responseStream.flush();
            responseStream.close();
            context.renderResponse();
            context.responseComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
